﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Constant;
using System.Windows.Forms;

namespace RockCompany.Master.Model
{
    public class SI02BFormModel : CommonModel
    {
        #region Properties
        private DataTable matsOrderList;
        public DataTable MatsOrderList 
        {
            get { return matsOrderList; }
            set { matsOrderList = value; }
        }

        private DataTable matsOrderDetail;
        public DataTable MatsOrderDetail 
        {
            get { return matsOrderDetail; }
            set { matsOrderDetail = value; } 
        }

        #endregion

        #region Constructors
        public SI02BFormModel()
        {
            dbConnector = new DBConnector();
        }
        #endregion

        #region Utility Methods
        #endregion

        #region Main Methods
        public DataTable searchWorkOrder(string sFromDate, string sToDate)
        {
            string sSql = "SELECT *";
            sSql += " FROM " + PHIEU_LANH_NVL_DTO.N_TABLE;
            sSql += " WHERE ";
            sSql += " CONVERT(VARCHAR," + PHIEU_LANH_NVL_DTO.N_CREATION_TIME + ",112) BETWEEN " + sFromDate + " AND " + sToDate;
            matsOrderList = selectData(sSql).Copy();
            return matsOrderList;
        }

        public DataTable searchWorkOrderDetails(string matOrderNo)
        {
            string sSql = "SELECT CONVERT(BIT,'0') AS CHON,*";
            sSql += " FROM " + PHIEU_LANH_NVL_CT_DTO.N_TABLE;
            sSql += " WHERE " + PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL + "='" + matOrderNo + "'";
            matsOrderDetail = selectData(sSql).Copy();
            return matsOrderDetail;
        }

        public bool removeMatOrder(string sMatOder)
        {
            string sClause = " WHERE " + PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL + " = '" + sMatOder + "'";
            return removeRow(PHIEU_LANH_NVL_DTO.N_TABLE, sClause, null);
        }

        public bool removeMatOrderDetail(string sMatOder)
        {
            string sClause = " WHERE " + PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL + " = '" + sMatOder + "'";
            return removeRow(PHIEU_LANH_NVL_CT_DTO.N_TABLE, sClause, null);
        }

        public bool export2Excel(string newPath, PHIEU_LANH_NVL_DTO matOrder)
        {
            bool b = false;
            ExcelExporter excelExport = new ExcelExporter();
            excelExport.Trans = this.translator;
            if (excelExport.open(SI02BConst.EX_NAME))
            {
                excelExport.inputValue(SI02BConst.EX_POS_MS_CT_VAL, matOrder.MS_PHIEU_LANH_NVL);
                excelExport.inputValue(SI02BConst.EX_POS_DON_VI_LANH_VAL, matOrder.NGUOI_LANH_NVL);
                excelExport.inputValue(SI02BConst.EX_POS_NGAY_LANH_VAL, matOrder.NGAY_LANH_NVL.ToShortDateString());

                List<DataColumn> disCol = new List<DataColumn>();
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU]);
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_SO_LO]);
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_DON_VI]);
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH]);
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH]);
                disCol.Add(matsOrderDetail.Columns[PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU]);

                excelExport.inputRangeValues(matsOrderDetail, disCol, SI02BConst.EX_BEGIN_GRID_COL, SI02BConst.EX_BEGIN_GRID_ROW, true);
                excelExport.inputValue(SI02BConst.EX_POS_PHE_CHUAN + Convert.ToString(matsOrderDetail.Rows.Count + SI02BConst.EX_BEGIN_GRID_ROW + 1), SI02BConst.EX_VAL_PHE_CHUAN);
                excelExport.inputValue(SI02BConst.EX_POS_THU_KHO + Convert.ToString(matsOrderDetail.Rows.Count + SI02BConst.EX_BEGIN_GRID_ROW + 1), SI02BConst.EX_VAL_THU_KHO);
                excelExport.inputValue(SI02BConst.EX_POS_NGUOI_LANH + Convert.ToString(matsOrderDetail.Rows.Count + SI02BConst.EX_BEGIN_GRID_ROW + 1), SI02BConst.EX_VAL_NGUOI_LANH);
                b = excelExport.saveExcel(newPath);
                excelExport.close();
            }
            return b;
        }
        #endregion
    }
}
